package com.warehouse.module.business.controller.admin.cargoinfo;

import com.google.common.collect.Maps;
import com.warehouse.framework.security.core.util.SecurityFrameworkUtils;
import com.warehouse.module.business.controller.admin.cargofile.vo.CargoFileReqVO;
import com.warehouse.module.business.dal.dataobject.cargofile.CargoFileDO;
import com.warehouse.module.business.enums.FileTypes;
import com.warehouse.module.business.service.cargofile.CargoFileService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import com.warehouse.framework.common.pojo.PageParam;
import com.warehouse.framework.common.pojo.PageResult;
import com.warehouse.framework.common.pojo.CommonResult;
import com.warehouse.framework.common.util.object.BeanUtils;
import static com.warehouse.framework.common.pojo.CommonResult.success;

import com.warehouse.framework.excel.core.util.ExcelUtils;

import com.warehouse.framework.apilog.core.annotation.ApiAccessLog;
import static com.warehouse.framework.apilog.core.enums.OperateTypeEnum.*;

import com.warehouse.module.business.controller.admin.cargoinfo.vo.*;
import com.warehouse.module.business.dal.dataobject.cargoinfo.CargoInfoDO;
import com.warehouse.module.business.service.cargoinfo.CargoInfoService;

@Tag(name = "管理后台 - 货物资料")
@RestController
@RequestMapping("/business/cargo-info")
@Validated
public class CargoInfoController {

    @Resource
    private CargoInfoService cargoInfoService;

    @Resource
    private CargoFileService cargoFileService;

    @PostMapping("/create")
    @Operation(summary = "创建货物资料")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:create')")
    public CommonResult<Long> createCargoInfo(@Valid @RequestBody CargoInfoSaveReqVO createReqVO) {
        createReqVO.setServiceId(SecurityFrameworkUtils.getLoginUserId());
        if(createReqVO.getServiceGrantStatus() == 0){
            createReqVO.setServiceOtherId(null);
        }
        return success(cargoInfoService.createCargoInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新货物资料")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:update')")
    public CommonResult<Boolean> updateCargoInfo(@Valid @RequestBody CargoInfoSaveReqVO updateReqVO) {
        if(updateReqVO.getServiceGrantStatus() == 0){
            updateReqVO.setServiceOtherId(null);
        }
        cargoInfoService.updateCargoInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除货物资料")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:cargo-info:delete')")
    public CommonResult<Boolean> deleteCargoInfo(@RequestParam("id") Long id) {
        cargoInfoService.deleteCargoInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得货物资料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:query')")
    public CommonResult<CargoInfoRespVO> getCargoInfo(@RequestParam("id") Long id) {
        return success(cargoInfoService.getCargoInfo(id));
    }

    @GetMapping("/sumary")
    @Operation(summary = "获取统计信息")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:query')")
    public CommonResult<List<CargoInfoSumaryVO>> suamryInfo(CargoInfoPageReqVO pageReqVO) {
        return success(cargoInfoService.sumaryOrderStatus(pageReqVO));
    }

    @GetMapping("/queryCreateFile/{id}")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:query')")
    public CommonResult<Map<String, Object>> queryCreateFile(@PathVariable("id") Long id) {
        CargoFileReqVO req1 = new CargoFileReqVO();
        req1.setCargoId(id);
        req1.setFileType(FileTypes.SHENBAO_FILES.getCode());
        List<CargoFileDO> shenbaoFiles = cargoFileService.getCargoFileList(req1);

        req1.setFileType(FileTypes.ZHIDAN_FILES.getCode());
        List<CargoFileDO> zhidanFiles = cargoFileService.getCargoFileList(req1);

        req1.setFileType(FileTypes.BAOGUAN_FILES.getCode());
        List<CargoFileDO> baoguanFiles = cargoFileService.getCargoFileList(req1);

        req1.setFileType(FileTypes.JIANDING_FILES.getCode());
        List<CargoFileDO> jiandingFiles = cargoFileService.getCargoFileList(req1);

        req1.setFileType(FileTypes.DGD_FILES.getCode());
        List<CargoFileDO> dgdFiles = cargoFileService.getCargoFileList(req1);

        Map<String, Object> result = Maps.newHashMap();
        result.put("shenbaoFiles", shenbaoFiles);
        result.put("zhidanFiles", zhidanFiles);
        result.put("baoguanFiles", baoguanFiles);
        result.put("jiandingFiles", jiandingFiles);
        result.put("dgdFiles", dgdFiles);
        return success(result);
    }

    @GetMapping("/page")
    @Operation(summary = "获得货物资料分页")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:query')")
    public CommonResult<PageResult<CargoInfoRespVO>> getCargoInfoPage(@Valid CargoInfoPageReqVO pageReqVO) {
        PageResult<CargoInfoRespVO> pageResult = cargoInfoService.getCargoInfoPage(pageReqVO);
        return success(pageResult);
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出货物资料 Excel")
//    @PreAuthorize("@ss.hasPermission('business:cargo-info:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportCargoInfoExcel(@Valid CargoInfoPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<CargoInfoDO> list = cargoInfoService.getCargoInfoPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "货物资料.xls", "数据", CargoInfoRespVO.class,
//                        BeanUtils.toBean(list, CargoInfoRespVO.class));
//    }


    @PostMapping("/receipt")
    @Operation(summary = "创建货物资料")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:receipt')")
    public CommonResult<Boolean> addCargoReceipt(@RequestBody CargoReceiptVO createReqVO) {
        cargoInfoService.receiptInput(createReqVO);
        return success(true);
    }

    @GetMapping("/queryReceipt/{id}")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:query')")
    public CommonResult<Map<String, Object>> receiptQuery(@PathVariable("id") Long id) {
        CargoFileReqVO req1 = new CargoFileReqVO();
        req1.setCargoId(id);
        req1.setFileType(FileTypes.RECEIPT_INPUT.getCode());
        List<CargoFileDO> receiptList = cargoFileService.getCargoFileList(req1);

        Map<String, Object> result = Maps.newHashMap();
        result.put("receiptList", receiptList);
        return success(result);
    }

    @PostMapping("/pack")
    @Operation(summary = "货物打包")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:pack')")
    public CommonResult<Boolean> addCargoReceipt(@RequestBody CargoPackVO createReqVO) {
        cargoInfoService.packGoods(createReqVO);
        return success(true);
    }

    @GetMapping("/querypack/{id}")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:query')")
    public CommonResult<Map<String, Object>> packQuery(@PathVariable("id") Long id) {
        CargoFileReqVO req1 = new CargoFileReqVO();
        req1.setCargoId(id);
        req1.setFileType(FileTypes.PACKAGE_GOODSS.getCode());
        List<CargoFileDO> packList = cargoFileService.getCargoFileList(req1);

        req1.setFileType(FileTypes.RECEIPT_INPUT.getCode());
        List<CargoFileDO> receiptList = cargoFileService.getCargoFileList(req1);

        Map<String, Object> result = Maps.newHashMap();
        result.put("packList", packList);
        result.put("receiptList", receiptList);
        return success(result);
    }

    @PostMapping("/packCheck")
    @Operation(summary = "包装审核")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:packCheck')")
    public CommonResult<Boolean> packCheck(@Valid @RequestBody cargoInfoPackCheckVO checkVO) {
        cargoInfoService.packCheck(checkVO);
        return success(true);
    }

    @PostMapping("/addPlan")
    @Operation(summary = "添加计划")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:addplan')")
    public CommonResult<Boolean> deleteCargoInfo(@RequestBody CargoInfoAddPlanVO vo) {
        cargoInfoService.addPlan(vo);
        return success(true);
    }

    @PostMapping("/shipment")
    @Operation(summary = "仓库出仓")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:shipment')")
    public CommonResult<Boolean> addCargoShipment(@RequestBody CargoShipmentVO createReqVO) {
        cargoInfoService.shipmentGoods(createReqVO);
        return success(true);
    }

    @PostMapping("/scene")
    @Operation(summary = "安检")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:scene')")
    public CommonResult<Boolean> addCargoScene(@RequestBody CargoShipmentVO createReqVO) {
        cargoInfoService.SceneGoods(createReqVO);
        return success(true);
    }


    @GetMapping("/queryshipment/{id}")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:query')")
    public CommonResult<Map<String, Object>> queryshipment(@PathVariable("id") Long id) {
//        CargoInfoDO cargoInfo = cargoInfoService.getCargoInfo(id);
        CargoFileReqVO req1 = new CargoFileReqVO();
        req1.setCargoId(id);
        req1.setFileType(FileTypes.OUTBOUND.getCode());
        List<CargoFileDO> shipmentList = cargoFileService.getCargoFileList(req1);

        req1.setFileType(FileTypes.WEIGHING_STRIP.getCode());
        List<CargoFileDO> stripList = cargoFileService.getCargoFileList(req1);

        Map<String, Object> result = Maps.newHashMap();
//        result.put("cargoInfo", cargoInfo);
        result.put("shipmentList", shipmentList);
        result.put("stripList", stripList);
        return success(result);
    }

    @PostMapping("/customs")
    @Operation(summary = "报关信息")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:customs')")
    public CommonResult<Boolean> addCargoCustoms(@RequestBody CargoCustomsVO createReqVO) {
        cargoInfoService.addCustoms(createReqVO);
        return success(true);
    }

    @PostMapping("/handUp")
    @Operation(summary = "交单")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:handUp')")
    public CommonResult<Boolean> addCargoHandUp(@RequestBody CargoShipmentVO createReqVO) {
        cargoInfoService.addHandUp(createReqVO);
        return success(true);
    }

    @PostMapping("/airway")
    @Operation(summary = "报关信息")
    @PreAuthorize("@ss.hasPermission('business:cargo-info:airway')")
    public CommonResult<Boolean> addCargoAirway(@RequestBody CargoAirwayVO createReqVO) {
        cargoInfoService.addAirway(createReqVO);
        return success(true);
    }

//    @GetMapping("/querycustoms/{id}")
//    @PreAuthorize("@ss.hasPermission('business:cargo-info:queryCustoms')")
//    public CommonResult<Map<String, Object>> querycustoms(@PathVariable("id") Long id) {
////        CargoInfoDO cargoInfo = cargoInfoService.getCargoInfo(id);
//        CargoFileReqVO req1 = new CargoFileReqVO();
//        req1.setCargoId(id);
//        req1.setFileType(FileTypes.CUSTOMS_INFOMATION.getCode());
//        List<CargoFileDO> customsList = cargoFileService.getCargoFileList(req1);
//
//        req1.setFileType(FileTypes.FINISH_OTHERS.getCode());
//        List<CargoFileDO> othersList = cargoFileService.getCargoFileList(req1);
//
//        Map<String, Object> result = Maps.newHashMap();
////        result.put("cargoInfo", cargoInfo);
//        result.put("customsList", customsList);
//        result.put("othersList", othersList);
//        return success(result);
//    }

//    @PostMapping("/arrival")
//    @Operation(summary = "物资到港")
//    @PreAuthorize("@ss.hasPermission('business:cargo-info:arrival')")
//    public CommonResult<Boolean> addCargoArrival(@RequestBody CargoArrivalVO cargoArrivalVO) {
//        cargoInfoService.arrival(cargoArrivalVO);
//        return success(true);
//    }
//
//    @PostMapping("/shippedBack")
//    @Operation(summary = "待发货打回")
//    @PreAuthorize("@ss.hasPermission('business:cargo-info:shippedBack')")
//    public CommonResult<Boolean> addCargoCustoms(@RequestBody ShippedBackVO shippedBackVO) {
//        cargoInfoService.shippedBack(shippedBackVO);
//        return success(true);
//    }
//
//    @GetMapping("/queryFile/{id}")
//    @PreAuthorize("@ss.hasPermission('business:cargo-info:queryFile')")
//    public CommonResult<Map<String, Object>> queryFile(@PathVariable("id") Long id) {
//        CargoFileReqVO req1 = new CargoFileReqVO();
//        req1.setCargoId(id);
//        List<CargoFileDO> fileList = cargoFileService.getCargoFileList(req1);
//
//        List<String> receiptList = fileList.stream().filter(it->it.getFileType().equals(FileTypes.RECEIPT_INPUT.getCode()))
//                .map(CargoFileDO::getFileId).collect(Collectors.toList());
//        List<String> packList = fileList.stream().filter(it->it.getFileType().equals(FileTypes.PACKAGE_GOODSS.getCode()))
//                .map(CargoFileDO::getFileId).collect(Collectors.toList());
//        List<String> outboundList = fileList.stream().filter(it->it.getFileType().equals(FileTypes.OUTBOUND.getCode()))
//                .map(CargoFileDO::getFileId).collect(Collectors.toList());
//        List<String> stripList = fileList.stream().filter(it->it.getFileType().equals(FileTypes.WEIGHING_STRIP.getCode()))
//                .map(CargoFileDO::getFileId).collect(Collectors.toList());
//        List<String> customsList = fileList.stream().filter(it->it.getFileType().equals(FileTypes.CUSTOMS_INFOMATION.getCode()))
//                .map(CargoFileDO::getFileId).collect(Collectors.toList());
//        List<String> othersList = fileList.stream().filter(it->it.getFileType().equals(FileTypes.FINISH_OTHERS.getCode()))
//                .map(CargoFileDO::getFileId).collect(Collectors.toList());
//
//        Map<String, Object> result = Maps.newHashMap();
//        result.put("receiptList", receiptList);
//        result.put("packList", packList);
//        result.put("outboundList", outboundList);
//        result.put("stripList", stripList);
//        result.put("customsList", customsList);
//        result.put("othersList", othersList);
//        return success(result);
//    }

}